﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wedo.Recognition.Domain.Models;
using Wedo.Mvc.Utility;
using Wedo.Domain.Models;
using Wedo.Mvc.Pager;
using Wedo.Recognition.Domain; 
namespace Wedo.Recognition.Web.Controllers
{
    public class PaymentAttachController :BaseController
    {
        private EFDbContext db = new EFDbContext();
 
 
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        /// <summary>
        /// 主页
        /// </summary>
        /// <returns></returns>
        public virtual ActionResult Index()
        {
            return Search(new CommonTimeSearch { });
        }

        /// <summary>
        /// 搜索方法
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public virtual ActionResult Search(CommonTimeSearch where)
        {
            where = where ?? new CommonTimeSearch();
            ViewData["SearchModel"] = where;

            var list = GetSearchPagedList(where);
            return ReturnAjaxView(list);
        }

        protected PagedList<PaymentAttach> GetSearchPagedList(CommonTimeSearch where)
        {
            IQueryable<PaymentAttach> query = db.PaymentAttach;

            if (where.StartTime != null)
            {
                DateTime Start = where.StartTime.Value;
                query = query.Where(m => m.Created > Start);
            }
            if (where.EndTime != null)
            {
                DateTime endTime = where.EndTime.Value.AddDays(1);
                query = query.Where(m => m.Created < endTime);
            }
            if (where.Keyword.IsNotNullOrWhiteSpace())
                query = query.Where(m => m.FileName.Contains(where.Keyword));

            return query.OrderByDescending(m=>m.Created).ToPagedList(where.Page, 10);
        }

        public ActionResult Download(Guid id)
        {
            var model = db.PaymentAttach.Find(id);
            var path= Wedo.Mvc.Utility.Common.FileHelpers.GetRecivePath(model.FilePath);
            string name = System.IO.Path.GetFileName(path);
            return File(path, "application/octet-stream", name);
        }
    }
}